package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sport;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Reference
    private FoodService foodService;


    /**
     * 上传文件/批量导入运动信息
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);//使用POI解析表格数据
            List<Food> data = new ArrayList<>();
            for (String[] strings : list) {
                String id = strings[0];
                String code = strings[1];
                String name = strings[2];
                String remark = strings[3];
                String unit = strings[4];
                String age = strings[5];
                Food food = new Food(Integer.parseInt(id), code, name, remark, unit, age);
                data.add(food);
            }
            foodService.insertFood(data);
            return new Result(true, "批量导入食物数据成功");
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, "批量导入食物数据失败");
        }


    }


    /**
     * 分页查询膳食库
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return foodService.findpage(queryPageBean);
    }


    /**
     * 新增膳食库信息
     */
    @RequestMapping("/addFood")
    public Result addFood(@RequestBody Food food) {
        try {
            foodService.addFood(food);
            return new Result(true, "新增膳食库成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增膳食库失败");
        }
    }


    /**
     * 根据id获取食物的的信息
     *
     * @param id
     * @return
     */
    @GetMapping("/findFoodById")
    public Result findFoodById(Integer id) {

        try {
            Food food = foodService.findFoodById(id);
            return new Result(true, "查询食物信息成功", food);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询食物信息失败");
        }
    }


    /**
     * 编辑食物信息信息
     *
     * @param food
     * @return
     */
    @RequestMapping("/editFood")
    public Result editFood(@RequestBody Food food) {
        try {
            foodService.editFood(food);
            return new Result(true, "编辑食物信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "编辑食物信息失败");
        }
    }


    /**
     * 根据id删除食物信息
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteFoodById")
    public Result deleteFoodById(Integer id) {
        try {
            foodService.deleteFoodById(id);
            return new Result(true, "删除食物信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除信息失败");
        }
    }
}
